package com.ran.sys.entity.controller;


import com.ran.common.vo.Result;
import com.ran.sys.entity.User;
import com.ran.sys.entity.UserStatus;
import com.ran.sys.mapper.StatusMapper;
import com.ran.sys.mapper.UserMapper;
import com.ran.sys.service.IFaceService;
import com.ran.sys.service.IStatusService;
import com.ran.sys.service.impl.FaceServiceImpl;
import com.ran.sys.service.impl.StatusServiceImpl;
import com.ran.sys.service.impl.UserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.util.*;

@RequiredArgsConstructor
@RestController
@RequestMapping("/face")
public class FaceController {

    @Autowired
    private FaceServiceImpl ifaceService;

    @Autowired
    private StatusServiceImpl statusService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StatusMapper statusMapper;

    @PostMapping("/check")
    public Result<Map<String, Object>> facecheck(@RequestParam MultipartFile file){
        File targetFile = ifaceService.dealFile(file);
        if (targetFile == null) {
            return Result.fail("file为空！");
        }
        //储存特征模板
        String template = ifaceService.sendRequest3(targetFile);
        //返回判定用户,0为空，否则其他为用户信息
        User user=ifaceService.judgingUser(template);
        if (user==null) {
            statusService.save(new UserStatus(null,"失败",null, LocalDateTime.now()));
            return Result.fail("找不到此人");
        }


        Map<String,Object> map=new HashMap<>();
        statusService.save(new UserStatus(null,"成功",user.getUsername(), LocalDateTime.now()));
        map.put("识别人脸",user.getUsername());
        return Result.success(map);
    }

}
